/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI.Facturacion;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 *
 * @author caro
 */
public class FormularioPagoFactura extends JDialog implements ActionListener{
    private JTextField tipoMP;
    private JTextField numCuotasMP;
    private JTextField valorMP;
    private JLabel lMP;
    private JButton bAgregarPago;
    private JButton bVerificarPago;
    private JTextArea areaPagos;
    private ArrayList<String[]> pagos;
    
    public FormularioPagoFactura(JDialog framePadre, boolean modal){
        super(framePadre, modal);
        setTitle("Cajero Cocos - Medios de Pago");
        
        //Image icono = Toolkit.getDefaultToolkit().getImage("images/logo25x25.jpg");
        //setIconImage(icono);
        inicializarComponentes();
        organizarComponentes();
        pack();
        setResizable(true);
        setVisible(true);
    }
    
    private void inicializarComponentes() {
        lMP = new JLabel("Pagos", new ImageIcon("/GUI/images/shopping-bag-96x96.png"), SwingConstants.LEFT);
	lMP.setFont(new Font("Arial", Font.BOLD, 18));
	lMP.setForeground(new Color(78,79,99,190));
                
        tipoMP = new JTextField();
        numCuotasMP = new JTextField();
        valorMP = new JTextField();
        
        bAgregarPago = new JButton("Agregar Pago");
        configurarBoton(bAgregarPago);
        
        bVerificarPago = new JButton("Verificar Pago");
        configurarBoton(bVerificarPago);
        
        areaPagos = new JTextArea();
        areaPagos.setEditable(false);
        
        pagos=new ArrayList<>();
    }
    
    private void configurarBoton(JButton boton) {
        boton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        boton.setOpaque(true);
	boton.setBackground(new Color(0,0,0,0));
	boton.addActionListener(this);
    }
    
    private void organizarComponentes() {
        /* ****************** Parte Norte Pestanna */
        JPanel pTitulo = new JPanel();
	pTitulo.setBackground(new Color(0,0,0,0));
	pTitulo.add(tipoMP);
        
        JPanel pEncabezado = new JPanel(new GridLayout(3,2));
        pEncabezado.setBackground(new Color(0,0,0,0));
        pEncabezado.add(new JLabel( "Tipo Medio  Pago:" ));
        pEncabezado.add(tipoMP);
        pEncabezado.add(new JLabel( "Número de Cuotas:" ));
        pEncabezado.add(numCuotasMP);
        pEncabezado.add(new JLabel( "Valor Pago:" ));
        pEncabezado.add(valorMP);
        
        JPanel pAdd = new JPanel();
	pAdd.setBackground(new Color(0,0,0,0));
	pAdd.add(bAgregarPago);
        
        JPanel pNorte = new JPanel(new BorderLayout(0,5));
        pNorte.setBorder(BorderFactory.createEmptyBorder(20,130,0,130));//n,w,s,e
	pNorte.setBackground(new Color(0,0,0,0));
	pNorte.add(pTitulo, BorderLayout.NORTH);
	pNorte.add(pEncabezado, BorderLayout.CENTER);
	pNorte.add(pAdd, BorderLayout.SOUTH);
	
        /* ****************** Parte Central Pestanna */
        JScrollPane scroll = new JScrollPane(areaPagos);
        setMinimumSize(new Dimension(200, 350));//h v
        
        JPanel pVerificar = new JPanel();
        pVerificar.setBackground(new Color(0,0,0,0));
        pVerificar.add(bVerificarPago);
                
        JPanel pCentro = new JPanel(new BorderLayout(0,5));
        pCentro.setBorder(BorderFactory.createEmptyBorder(20,80,10,80));//n,w,s,e
        pCentro.setBackground(new Color(0,0,0,0));
        pCentro.add(scroll, BorderLayout.CENTER);
        pCentro.add(pVerificar, BorderLayout.SOUTH);
        
        /* ****************** General Pestanna */
	this.add(pNorte, BorderLayout.NORTH);
	this.add(pCentro, BorderLayout.CENTER);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getSource()==bAgregarPago){
            agregarPago();
        }
        if(ae.getSource()==bVerificarPago){
            verificarPagos();
        }
    }

    private void agregarPago() {
        String[] unPago;
        unPago = new String[3];
        unPago[0]=(tipoMP.getText());
        unPago[1]=(numCuotasMP.getText());
        unPago[2]=(valorMP.getText());
        pagos.add(unPago);
        
        String stPago;
        stPago=unPago[0]+" "+unPago[1]+" "+unPago[2]+"\n";
        
        areaPagos.setText(areaPagos.getText()+stPago);
        limpiarCampos();
    }
    
    private void limpiarCampos() {
        tipoMP.setText("");
        numCuotasMP.setText("");
        valorMP.setText("");
    }

    private void verificarPagos() {
        int suma=0;
        for(int i=0; i<pagos.size(); i++){
            suma+=Integer.parseInt(pagos.get(i)[2]);
        }
        System.out.println("suma:"+suma);
        
        boolean ok;
        ok=true;//verificar acá
        if(ok){
            //si es mayor o igual al total, prosiga
            System.out.println("pagado:"+suma);
            pagos.clear();
            areaPagos.setText("");
            dispose();
        }
        else{
        }
    }
}
